package com.dreamoo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.dreamoo.common.enums.EmotionEnum;
import com.dreamoo.common.exception.BusinessException;
import com.dreamoo.common.pojo.dto.DreamAnalysisDTO;
import com.dreamoo.common.pojo.info.DreamAnalysInfo;
import com.dreamoo.common.pojo.vo.DreamVO;
import com.dreamoo.common.result.ResultCode;
import com.dreamoo.common.threadpool.DreamooThreadPoolConstant;
import com.dreamoo.common.threadpool.DreamooThreadPoolExecutor;
import com.dreamoo.common.threadpool.factory.DreamooThreadPoolExecutorFactory;
import com.dreamoo.service.ai.DreamAnalyst;
import com.dreamoo.service.AiDreamService;
import com.dreamoo.service.ai.ImageAnalyst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

/**
 * @author 山顶洞人郑某
 * @className AiDreamServiceImpl.java
 * @date 2025 05 09
 */
@Service
@Slf4j
public class AiDreamServiceImpl implements AiDreamService {



    @Autowired
    private DreamAnalyst dreamAnalyst;

    /**
     * 自定义线程池工厂
     */
    @Autowired
    private DreamooThreadPoolExecutorFactory dreamooThreadPoolExecutorFactory;
    /**
     * 梦境图片分析助手
     */
    @Autowired
    private ImageAnalyst imageAnalyst;
    /**
     * 解析梦境的主要结果
     *
     * @param dreamAnalysisDTO 梦境参数
     * @param userId 用户ID
     * @return 梦境解析结果
     */
    @Override
    public DreamVO analysisDream(DreamAnalysisDTO dreamAnalysisDTO, String userId) {
        if (Objects.isNull(dreamAnalysisDTO) || StrUtil.isEmpty(userId)) {
            throw new BusinessException(ResultCode.PARAM_MISS);
        }
        DreamVO dreamVO = new DreamVO();
        //生成图片任务
        CompletableFuture<String> imageTask = CompletableFuture.supplyAsync(() -> {
            //调用梦境图片生成大模型生成图片
            return imageAnalyst.createDreamImage(createUserMessage(dreamAnalysisDTO));
        }, dreamooThreadPoolExecutorFactory. //自定义线程池
                getCompletableExecutor(DreamooThreadPoolConstant.DREAM_PROCESSOR_POOL));


        //调用梦境分析大模型解析结果任务
        CompletableFuture<DreamAnalysInfo> dreamAnalysTask = CompletableFuture.
                supplyAsync(() -> dreamAnalyst.analysis(userId, createUserMessage(dreamAnalysisDTO)),
                dreamooThreadPoolExecutorFactory. //自定义线程池
                getCompletableExecutor(DreamooThreadPoolConstant.DREAM_PROCESSOR_POOL));

        //两个任务都完成的时候获取结果
        CompletableFuture.allOf(imageTask, dreamAnalysTask).thenRun(() -> {
            dreamVO.setImageUrl(imageTask.join());
            BeanUtils.copyProperties(dreamAnalysTask.join(),dreamVO);
        });

        log.info("AI梦境解析结果：{}",dreamVO);
        return dreamVO;
    }

    /**
     * 创建传入大模型的对话内容
     * @param dreamAnalysisDTO
     * @return
     */
    private String createUserMessage(DreamAnalysisDTO dreamAnalysisDTO) {
        String content = dreamAnalysisDTO.getContent(); //梦境内容
        String chat = dreamAnalysisDTO.getChat(); // 梦境随思
        EmotionEnum emotionTag = dreamAnalysisDTO.getEmotionTag();
        StringBuilder sb = new StringBuilder();

        sb.append("梦境内容：").append(content).append("\n");
        if (StrUtil.isNotEmpty(chat)) {
            sb.append("梦境随思：").append(chat).append("\n");
        }
        if (ObjectUtil.isNotEmpty(emotionTag)) {
            sb.append("梦境情绪：").append(emotionTag.getDesc()).append("\n");
        }
        return sb.toString();
    }
}
